package j.framework.tags.form;

import j.util.DateUtils;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import java.util.Date;

/**
 * 日期时间控件
 * User: jason
 * Date: 2014/12/18 10:11
 */
public class DateTag extends BaseInputTag {
    private String type;
    @Override
    public String getInputType() {
        setImeDisabled(true);
        return "date";
    }

    @Override
    public void writeInputTagValue(FormTagWriter writer, Object value) throws JspException {
        if(null == value){
            value = getFormDataValue();
        }
        if(null!=value && value instanceof Date){
            String _type = type;
            if(StringUtils.isNullOrEmpty(_type)){
                _type = "date";//默认仅为日期类型
            }
            Date date = (Date)value;
            String formatDate = null;
            if(_type.equals("date")){
                formatDate = DateUtils.format(date, "yyyy-MM-dd");
            }else if(_type.equals("datetime")){
                formatDate = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss");
            }else if(_type.equals("friendly")){
                formatDate = DateUtils.toDateDiffString(date);
            }
            if(null!=formatDate){
                writer.writeFormTagProperty("value",formatDate);
            }
        }
    }

    @Override
    public String getCssClassName() {
        return "easyui-datebox";
    }

    public void setType(String type) {
        this.type = type;
    }
}
